/**
 * Created by ROGSTAR on 2016/9/30.
 */
/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');

    var datepicker = require('datepicker');
    var toastr =require('toastr');

    app.controller('moneyCtrl', ['$scope','$http','$filter',function ($scope, $http,$filter) {
        datepicker($scope);


        $scope.getInfo= function () {
            $http.post("/admin/commission/list",{userType:$scope.condition.userType})
                .success(function (data){
                    console.log(data)
                    for(var i=0;i<data.length;i++){
                        if(data[i].level==1){
                            $scope.condition.commissionRate1=data[i].commissionRate;
                            $scope.condition.id1=data[i].id;
                        }
                        if(data[i].level==2){
                            $scope.condition.commissionRate2=data[i].commissionRate;
                            $scope.condition.id2=data[i].id;
                        }
                        if(data[i].level==3){
                            $scope.condition.commissionRate3=data[i].commissionRate;
                            $scope.condition.id3=data[i].id;
                        }
                        if(data[i].level==0){
                            $scope.condition.commissionRate0=data[i].commissionRate;
                            $scope.condition.id0=data[i].id;
                        }

                        $scope.condition.giveType=data[i].giveType;
                        $scope.condition.giveDay=data[i].giveDay

                    }
                }).error(function(data){
                });
        };



        $scope.upDate= function () {
            if ($scope.condition.giveDay>31 || $scope.condition.giveDay<=0 || $scope.condition.commissionRate1>10 || $scope.condition.commissionRate1<0|| $scope.condition.commissionRate2>10 || $scope.condition.commissionRate2<0|| $scope.condition.commissionRate3>10 || $scope.condition.commissionRate3<0|| $scope.condition.commissionRate0>10 || $scope.condition.commissionRate0<0) {
                toastr.error("信息填写有误");
                return;
            }
            if(!$scope.condition.userType){
                toastr.error("请选择用户属性");
                return;
            }
            var param1={
                commissionRate: $scope.condition.commissionRate1,
                giveDay:$scope.condition.giveDay,
                giveType:$scope.condition.giveType,
                level: 1,
                userType: $scope.condition.userType,
                id:$scope.condition.id1
            };
            var param2={
                commissionRate: $scope.condition.commissionRate2,
                giveDay:$scope.condition.giveDay,
                giveType:$scope.condition.giveType,
                level: 2,
                userType: $scope.condition.userType,
                id:$scope.condition.id2
            };
            var param3={
                commissionRate: $scope.condition.commissionRate3,
                giveDay:$scope.condition.giveDay,
                giveType:$scope.condition.giveType,
                level: 3,
                userType: $scope.condition.userType,
                id:$scope.condition.id3
            };
            var param0={
                commissionRate: $scope.condition.commissionRate0,
                giveDay:$scope.condition.giveDay,
                giveType:$scope.condition.giveType,
                level: 0,
                userType: $scope.condition.userType,
                id:$scope.condition.id0
            };
            var paramArr=[param1,param2,param3,param0];
            console.log(paramArr);
            for(var i=0;i<paramArr.length;i++){
                $http.post("/admin/commission/update",paramArr[i])
                    .success(function (data){
                        console.log(data);
                            if(data.result=='success'){
                                //toastr.success('修改成功')
                            }else{
                                toastr.error('修改失败')
                            }


                    }).error(function(data){
                    });
            }

            toastr.success('修改成功')

        }






    }]);


});